//
//  main.c
//  C_8高级函数
//
//  Created by Robin on 16/3/18.
//  Copyright © 2016年 cnbm. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void find(int(*match)(char *s));
int sport_art (char *s);
int sport_art_dining (char *s);

int compare_scores      (const void *score1,const void *score2); // 升序
int compare_scores_desc (const void *score1,const void *score2); // 降序

typedef struct {
    float height;
    float width;
}rectangle;

int compare_areas      (const void *area1, const void *area2);   // 比较面积

int compare_str        (const void *str1,const void *str2);      // 字母排序

int compare_str_desc (const void *str1 , const void *str2);


// 函数指针
char *man[] = {
    " William: SBM GSOH like sports,TV,dining",
    " Matt: SWN NS like art,movies,theater",
    " Lius: SLD NM like books,theater,art",
    " Mike: DWM DS likes trucks, sports and bieber",
    " Peter: SAM likes chess, working out and art",
    " Josh: SJM likes sports",
    " Jed: DBM likes theater, books and dining"
};

int main(int argc, const char * argv[]) {
    
    int scores[] = {543,323,32,554,11,3,122};
    
    for (int i=0; i<7; i++) {
        printf("num is %d\n",scores[i]);
    }
    
    puts("================================");

    
    qsort(scores, 7, sizeof(int), compare_scores);
    
    for (int i=0; i<7; i++) {
        printf("num is %d\n",scores[i]);
    }
    
    puts("================================");
    
    qsort(scores, 7, sizeof(int), compare_scores_desc);
    
    for (int i=0; i<7; i++) {
        printf("num is %d\n",scores[i]);
    }
    
    puts("================================");

    char *str_arr[] = {"zye","abc","bcd","ecs","aaa"};
    
    for (int i=0; i<5; i++) {
        printf("num is %s\n",str_arr[i]);
    }
    
    puts("================================");

    qsort(str_arr, 5, sizeof(str_arr[0]), compare_str);
    
    for (int i=0; i<5; i++) {
        printf("num is %s\n",str_arr[i]);
    }
    
    puts("================================");

    qsort(str_arr, 5, sizeof(str_arr[0]), compare_str_desc);
    
    for (int i=0; i<5; i++) {
        printf("num is %s\n",str_arr[i]);
    }
    
    puts("================================");

    
    //printf("num is %c\n",scores);
    
}

void find(int(*match)(char *s)){
    int NUM_ADS = 7;
    for (int i=0; i<NUM_ADS; i++) {
        if (match(man[i])) {
            puts("--------------");
            
            printf("ADS IS %s\n",man[i]);
            
            puts("--------------");

        }
    }
}

int compare_scores (const void *score1,const void *score2){
    int a = *(int *)score1; // 先把 void * 指针转为整型指针 再取值
    int b = *(int *)score2;
    return a-b;
}

int compare_scores_desc (const void *score1 , const void *scroe2){
    int a = *(int *)score1;
    int b = *(int *)scroe2;
    return b-a;
}

int compare_area  (const void *area1 , const void *area2){
    
    float area_pre = ((*(rectangle *)area1).width) * ((*(rectangle *)area1).height);
    float area_next = ((*(rectangle *)area2).width) * ((*(rectangle *)area2).height);
    return area_pre - area_next;
}

int compare_area_desc (const void *area1 , const void *area2){
    return compare_area(area2, area1);    // 面积降序
}

int compare_str (const void *str1 , const void *str2){
    
    char *a = (char *)str1;
    char *b = (char *)str2;
    return strcmp(a, b);
}

int compare_str_desc (const void *str1 , const void *str2){
    
    char *a = (char *)str1;
    char *b = (char *)str2;
    return strcmp(b, a);
}

int sport_art (char *s){
    return strstr(s, "sports") && !strstr(s, "bieber");
}

int sport_art_dining (char *s){
    return strstr(s, "sports") || strstr(s, "art") || strstr(s, "dining");
}
